package corp.edu.amadeus.core.business.service.impl;

import java.util.Collection;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import corp.edu.amadeus.core.business.object.impl.ContentBO;
import corp.edu.amadeus.core.business.object.impl.CompTextBO;
import corp.edu.amadeus.core.business.object.impl.FileBO;
import corp.edu.amadeus.core.business.object.impl.TextBO;
import corp.edu.amadeus.core.business.object.impl.CompFileBO;
import corp.edu.amadeus.core.business.service.ifc.IContentService;
import corp.edu.amadeus.core.facade.ifc.IGenericCRUDFacade;
import corp.edu.amadeus.core.facade.ifc.IGenericSearchFacade;
import corp.edu.amadeus.core.business.locator.ObjectLocator;
import corp.edu.amadeus.utils.date.DateUtils;

public class ContentServiceImpl implements IContentService {

	private static Logger log = Logger.getLogger(ContentServiceImpl.class);
	private IGenericCRUDFacade cFac;
	private IGenericSearchFacade sFac;

	public ContentServiceImpl(IGenericCRUDFacade cFac,
			IGenericSearchFacade sFac) {
		this.cFac = cFac;
		this.sFac = sFac;
	}

	public ContentBO addContent(String desc, String type, String lang) {
		ContentBO c = ObjectLocator.getContent();
		c.setDateCreated(DateUtils.getNow());
		c.setDateModified(DateUtils.getNow());
		c.setDescription(desc);
		c.setLang(lang);
		c.setType(type);
		this.cFac.persist(c);
		this.cFac.flush();
		return c;
	}

	public ContentBO updateContent(int contentId, String desc,
			String type, String lang) throws Exception {
		ContentBO c = (ContentBO)this.sFac.findByKey(ContentBO.class,
					"persistenceId", contentId);
		c.setDateModified(DateUtils.getNow());
		c.setDescription(desc);
		c.setLang(lang);
		c.setType(type);
		this.cFac.update(c);
		this.cFac.flush();
		return c;
	}

	public void deleteContent(int contentId) throws Exception {
		ContentBO c = (ContentBO)this.sFac.findByKey(ContentBO.class,
					"persistenceId", contentId);
		this.cFac.delete(c);
		this.cFac.flush();
	}

	public void addTextComponent(int contentId, TextBO text,
			float x, float y, String name) throws Exception {
		log.debug(" + + + + ContentID: " + contentId);
		ContentBO c = (ContentBO)this.sFac.findByKey(ContentBO.class,
				"persistenceId", contentId);
		CompTextBO text_component = ObjectLocator.getTextComponent();
		text_component.setContent(c);
		text_component.setText(text);
		text_component.setX(x);
		text_component.setY(y);
		this.cFac.persist(text_component);
		this.cFac.update(c);
		this.cFac.update(text);
		this.cFac.flush();
		this.cFac.refresh(c);
		this.cFac.refresh(text);
		this.cFac.refresh(text_component);
	}

	public void deleteTextComponent(int contentId, int textId)
		throws Exception {
		ContentBO c = (ContentBO)this.sFac.findByKey(ContentBO.class,
				"persistenceId", contentId);
		c.deleteTextComponent(textId);
		this.cFac.update(c);
		this.cFac.flush();
	}

	public void addFileComponent(int contentId, FileBO file,
			float x, float y) throws Exception {
		ContentBO c = (ContentBO)this.sFac.findByKey(ContentBO.class,
				"persistenceId", contentId);
		CompFileBO file_component = ObjectLocator.getFileComponent();
		file_component.setContent(c);
		file_component.setFile(file);
		file_component.setX(x);
		file_component.setY(y);
		/**Esta linea no se si es necesaria, hay que hacer pruebas.*/
		c.addFileComponent(file_component);
		this.cFac.persist(file_component);
		this.cFac.update(c);
		this.cFac.update(file);
		this.cFac.flush();
	}

	public void deleteFileComponent(int contentId, int fileId)
		throws Exception {
		ContentBO c = (ContentBO)this.sFac.findByKey(ContentBO.class,
				"id", contentId);
		c.deleteFileComponent(fileId);
		this.cFac.update(c);
		this.cFac.flush();
	}

	public void deleteAllContents() {
		String query = "from ContentBO order by id asc";
		Collection<ContentBO> contents = this.sFac.findByQuery(query);
		for(ContentBO content : contents) {
			this.cFac.delete(content);
		}
		this.cFac.flush();
	}

	public Collection<ContentBO> getAllContents() {
		String query = "from ContentBO order by id desc";
		return this.sFac.findByQuery(query);
	}

	public ContentBO getContent(int contentId)
		throws Exception {
		return (ContentBO)this.sFac.findByKey(ContentBO.class,
				"persistenceId", contentId);
	}



}